
const app=getApp()
const baseUrl='https://bczct.hbwmxx.com';
function request({url,method,data}){
  wx.showLoading({
    title: '加载中',
    mask:true
  })
  return new Promise((resolve,reject)=>{
    let header={
      'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
    }
    if(wx.getStorageSync('token')){   //判断有没有token 如果有就带token
      // console.log('548784113151')
      header.token=wx.getStorageSync('token')
      // console.log(wx.getStorageSync('token'))
    }
    wx.request({
      url: baseUrl+url,
      method:method,
      data:data,
      header:header,
      success(res){
        //请求成功
        //判断响应状态码---errCode状态根据后端定义来判断
        // console.log(res)
        if(res.data.state==2&&res.data.msg=="没有登录"){
          // console.error("1111")
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration:2000,
            mask:true
          })
          wx.hideLoading()
          // console.log(app)
          setTimeout(function(){
            wx.reLaunch({
              url: '../index1/index1?type=noLogin',
              success(){
                wx.hideLoading()
              }
            })
          },1500)
          
        }else{
          resolve(res.data)
          wx.hideLoading()
        }
        
      },
      fail(error){
        wx.hideLoading()
        wx.showToast({
          title:'请求错误，请稍后再试',
          icon: 'none',
          duration: 2000
        })
        reject(error.data)
      }
    })
  })
}
module.exports={
  request
}